	#include("/common/include/include.html")
<div class="admin-main">
	<blockquote class="layui-elem-quote">
					<span>主页<span><span>/</span><a href="#(ctx??)/admin/module/getListPage">模块管理</a><span>/</span><span class="curr">模块</span>
					<div class="func-right">
						<a href="#(ctx??)/admin/module/getEditPage" class="layui-btn layui-btn-small">
							<i class="layui-icon">&#xe608;</i> 添加模块
						</a>
						<a href="javascript:void(0);" onclick="deleteAll()" class="layui-btn layui-btn-small">
							<i class="layui-icon">&#x1006;</i> 删除
						</a>
					</div>
	</blockquote>
	<div class="site-block-full navi-left">
		<fieldset><legend>菜单导航</legend>
			<ul id="menuTree"></ul>
		</fieldset>
	</div>
	<div class="site-block-full main-right">
		<fieldset><legend>数据列表</legend>
			<table class="site-table table-hover" lay-even>
				  <thead>
					    <tr><th style="width: 12px;padding-left: 15px;"><input type="checkbox" id="selected-all"></th><th>模块名称</th><th>模块描述</th><th>图标</th><th>排序</th><th>操作</th></tr> 
				  </thead>
				  <tbody id = "tableBody"></tbody>
			</table>           
		   <div class="admin-table-page"><div id="pages" class = "pages"></div></div>
		</fieldset>
	</div>
</div>
		<!--模板-->
		<script type="text/html" id="conTemp">
				{{# layui.each(d.data, function(index, item){ }}
					<tr>
						<td><input type="checkbox" class="icheckbox_flat-green" value="{{ item.id?item.id:"" }}"></td>
						<td>{{ item.name?item.name:"" }}</td>
						<td>{{ item.description?item.description:"" }}</td>
						<td>{{ item.image?item.image:"" }}</td>
						<td>{{ item.sort?item.sort:"" }}</td>
						<td>
							<a href="#(ctx??)/admin/module/getEditPage?parentid={{ item.id?item.id:"" }}" onclick="return addChildren('{{ item.parent_id}}')" class="layui-btn layui-btn-normal layui-btn-mini">添加子模块</a>
							<a href="#(ctx??)/admin/module/getEditPage?id={{ item.id?item.id:"" }}" class="layui-btn layui-btn-mini">编辑</a>
							<a href="javascript:void(0);" onclick="deleteModule('{{ item.id?item.id:"" }}')" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>
						</td>
					</tr>
				{{# }); }}
		</script>
<script>
layui.config({
	  base: ctx+'/common/js/'
}).use(['tree','ajax','paging'], function(){
		 ajax = layui.ajax;
		 tree = layui.tree;
		 paging = layui.paging();
		 refresh();
});
function refresh(){
	initTree();
	getPageData("");
}
//初始化左侧菜单树
function initTree(){
	$("#menuTree").html("")
	var d = ajax.ajaxFunc(ctx+"/admin/module/getModuleTree",null,"json",null);
	layui.tree({
	    elem: '#menuTree' //传入元素选择器
	    ,nodes: d.data
	    ,click: function(node){
	    	getPageData(node.id);
	    }  
	});
}
function getPageData(pid){
	paging.init({
		url: ctx+"/admin/module/listData",  //地址
		elem: '#tableBody',
		tempElem: '#conTemp',  //模块容器
		pageConfig: {        //分页参数配置
			elem: '#pages',  //分页容器
			pageSize: 10,     //分页大小
			pid:pid
		}
	});
}
//删除
function deleteModule(ids){
	parent.layer.confirm('确定要删除吗?', function(index){
		parent.layer.close(index);
		var r = ajax.ajaxFunc(ctx+"/admin/module/delete",{"ids":ids},"json",null);
		if(r.success){
			parent.layer.alert(r.message);
			refresh();
		}else{
			parent.layer.alert("操作失败!");
		}
		
	});       
	
}
//删除所选
function deleteAll(){
	var data = [];
	$.each($(":checked"),function(i,d){
		data.push($(d).val());
	});
	var ids = data.join(",");
	deleteModule(ids);
}
//检查添加孩子
//校验添加子菜单
function addChildren(p){
	if("#root"==p){
		return true;
	}else{
		parent.layer.alert("暂时只支持两级菜单!");
		return false;
	}
}
</script>